﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PanhuaWin.通用
{
    public partial class Form部门档案 : UIForm

    {
        public string 工厂编号;
        public string 部门编号;
        public string 部门名称;

        public Form部门档案()
        {
            InitializeComponent();
        }

        private void uiButton查询_Click(object sender, EventArgs e)
        {
            ListDepartment();
        }

        private void ListDepartment()
        {
            string sql;
            sql = @"SELECT   guid, WorkShopCode, WorkShopName, AuditDate, AuditStatus, AuditId, Auditor, Remark, CreateID, Creator, 
                CreateDate, ModifyID, Modifier, ModifyDate   FROM      Dic_WorkShop     where  WorkShopCode='" + 工厂编号 + "'      ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            this.treeView1.Nodes.Clear();
            if (tb.Rows.Count>0)
            { string 工厂名称;
                string 工厂guid;
                工厂名称 = tb.Rows[0]["WorkShopName"].ToString();
                工厂guid = tb.Rows[0]["guid"].ToString();
                TreeNode tn = new TreeNode(工厂名称);
                this.treeView1.Nodes.Add(tn);
                sql = @"  SELECT  guid, WorkShopGuid, DepartCode, DepartName, ParentGuid, NcCode, AuditDate, AuditStatus, AuditId, Auditor, 
                Remark, CreateID, Creator, CreateDate, ModifyID, Modifier, ModifyDate   
FROM      Dic_Depart where WorkShopGuid='" + 工厂guid + "' and ParentGuid is null  order by DepartCode ";
                DataTable tb2=pub.GetDataTable(pub.ConnectionString, sql);
                if (tb2.Rows.Count>0)
                {
                    for (int i=0;i<tb2.Rows.Count;i++)
                    { string departCode = tb2.Rows[i]["DepartCode"].ToString();
                        string departName  = tb2.Rows[i]["DepartName"].ToString();
                        string departGuid = tb2.Rows[i]["guid"].ToString();
                        TreeNode bntn = new TreeNode(departCode +  " " + departName);
                        //TreeNode bntn = new TreeNode();
                        bntn.Text = departCode + " " + departName;
                        bntn.Name = departCode;
                        tn.Nodes.Add(bntn);
                        ListDepartment(bntn, 工厂guid, departGuid);
                    }
                }
            this.treeView1.ExpandAll();
            }
        }

        private void ListDepartment(TreeNode tn, string 工厂guid,string parentGuid)
        {
           string sql = @"  SELECT  guid, WorkShopGuid, DepartCode, DepartName, ParentGuid, NcCode, AuditDate, AuditStatus, AuditId, Auditor, 
                Remark, CreateID, Creator, CreateDate, ModifyID, Modifier, ModifyDate   
FROM      Dic_Depart where WorkShopGuid='" + 工厂guid + "' and ParentGuid ='"+ parentGuid + "' ";
            DataTable tb2 = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb2.Rows.Count > 0)
            {
                for (int i = 0; i < tb2.Rows.Count; i++)
                {
                    string departCode = tb2.Rows[i]["DepartCode"].ToString();
                    string departName = tb2.Rows[i]["DepartName"].ToString();
                    string departGuid = tb2.Rows[i]["guid"].ToString();
                    TreeNode bntn2 = new TreeNode(departCode + " " + departName);
                    bntn2.Text = departCode + " " + departName;
                    bntn2.Name = departCode;
                    tn.Nodes.Add(bntn2);
                    ListDepartment(bntn2, 工厂guid, departGuid);
                }
            }
        }

        private void Form部门档案_Load(object sender, EventArgs e)
        {
            ListDepartment();
        }

       
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                if (e.Node.Nodes.Count == 0)
                {
                    部门编号 = e.Node.Name;
                    部门名称 = e.Node.Text.Replace(部门编号, "").Replace(" ", "");
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {

                }

            }
            catch
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
          
        }
    }
}
